#include "FPCH.h"

#include "FLoader.h"

namespace Font
{

	Loader::Loader(Library* a_Library)
	{
		m_Library = a_Library;
	}
	
	Loader::~Loader()
	{
		
	}

	Library* Loader::GetLibrary()
	{
		return m_Library;
	}

	Face* Loader::CreateFace(Family* a_Family, Metrics* a_Metrics)
	{
		Face* result = a_Family->CreateFace(this, a_Metrics);

		m_Faces[a_Family->GetName().GetData()].push_back(result);

		return result;
	}

	FaceHandle* Loader::FindFont(Family* a_Family, unsigned int a_Size, unsigned int a_Properties, CharMap* a_Map /*= NULL*/)
	{
		std::map<std::string, std::vector<Face*>>::iterator found = m_Faces.find(a_Family->GetName().GetData());
		if (found == m_Faces.end())
		{
			return NULL;
		}

		std::vector<Face*>& faces = found->second;

		FaceHandle* result = NULL;
		for (size_t i = 0; i < faces.size(); i++)
		{
			result = faces[i]->GetHandle(a_Size, a_Properties, a_Map);
			if (result)
			{
				return result;
			}
		}

		return NULL;
	}

	void Loader::SetCharMap(const tb::String& a_FamilyName, CharMap* a_Map)
	{
		//m_MapFamilyDefault[a_FamilyName.GetData()] = a_Map;
		m_MapFamilyDefault.insert(std::pair<std::string, CharMap*>(a_FamilyName.GetData(), a_Map));
	}

	CharMap* Loader::GetCharMap(const tb::String& a_FamilyName)
	{
		std::map<std::string, CharMap*>::iterator found = m_MapFamilyDefault.find(a_FamilyName.GetData());
		if (found != m_MapFamilyDefault.end())
		{
			return found->second;
		}

		return NULL;
	}

}; // namespace Font